<template>
    <div style="">
        <div class="container" v-show="!showEdit">
            <div class="handle-box">
                <!-- <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button> -->
                <el-input v-model="title" placeholder="标题" class="handle-input mr10"></el-input>
                <!-- <el-date-picker
                    v-model="createTime"
                    type="date"
                    placeholder="创建时间"
                    value-format="yyyy-MM-dd"
                    style="width: 150px; margin-right: 10px"
                >
                </el-date-picker> -->
                <el-date-picker
                    v-model="timeRange"
                    type="daterange"
                    range-separator="-"
                    start-placeholder="创建时间"
                    end-placeholder="创建时间"
                    value-format="yyyy-MM-dd"
                    style="width: 230px; margin-right: 10px"
                >
                </el-date-picker>
                <el-select v-model="status" placeholder="状态" class="handle-select mr10">
                    <el-option key="全部" label="全部" value="全部"></el-option>
                    <el-option key="0" label="未发布" value="0"></el-option>
                    <el-option key="1" label="已发布" value="1"></el-option>
                    <el-option key="-1" label="已撤回" value="-1"></el-option>
                </el-select>
                <el-input v-model="authorName" placeholder="作者" class="handle-input mr10"></el-input>
                <el-input v-model="from" placeholder="转自" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
            </div>
            <div style="display: flex; justify-content: flex-end; margin-bottom: 20px">
                <el-button type="primary" @click="addAccount">新建</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column type="index" :index="methodIndex" label="序号" width="55" align="center"></el-table-column>
                <el-table-column prop="title" label="标题"></el-table-column>
                <el-table-column prop="authorName" label="作者"></el-table-column>
                <el-table-column prop="from" label="转自"></el-table-column>
                <el-table-column label="创建时间" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.createTime | dateFilter }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="发布时间" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.releaseTime | dateFilter }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="状态" width="80" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.status | statusFilter }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="readNum" label="阅读量"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.status == 0">编辑</el-button>
                        <el-button
                            type="text"
                            @click="handlePre(scope.$index, scope.row)"
                            v-if="scope.row.status == 0 || scope.row.status == 1"
                            >预览</el-button
                        >
                        <el-button type="text" @click="handleAction(scope.row, '发布')" v-if="scope.row.status == 0">发布</el-button>
                        <el-button type="text" @click="handleAction(scope.row, '撤回')" v-if="scope.row.status == 1">撤回</el-button>
                        <el-button type="text" @click="handleAction(scope.row, '删除')" v-if="scope.row.status == 0">删除</el-button>
                        <el-button type="text" @click="handleRebuild(scope.$index, scope.row)" v-if="scope.row.status == -1"
                            >重建</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next,jumper"
                    :current-page="pageNo"
                    :page-size="pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <edit-health-banner v-if="showEdit" :editType="editType" :bannerId="bannerId" @back="back" @save="save"></edit-health-banner>

        <!-- 状态弹出框 -->
        <el-dialog class="popView" :visible.sync="isShowPop" v-if="isShowPop" :show-close="false" width="200px">
            <div class="main">
                <span class="title">请确认是否{{ action }}</span>
                <div class="btns">
                    <el-button @click="isShowPop = false">取消</el-button>
                    <el-button type="primary" @click="goAction">{{ action }}</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 查看弹出框 -->
        <el-dialog class="popView preView" :visible.sync="isShow" v-if="isShow" :show-close="false" width="260px">
            <div class="main" style="height: 500px; padding: 12px 0; border-radius: 30px">
                <span class="title">{{ selItem.title }}</span>
                <iframe class="web" style="width: 350px; transform: scale(0.7)" :srcdoc="content" frameborder="0"></iframe>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
import editHealthBanner from '../common/editHealthBanner';
// import EditHealthBanner from '../common/editHealthBanner.vue';
export default {
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            delList: [],

            title: null,
            createTime: null,
            status: null,
            authorName: null,
            from: null,
            pageNo: 1,
            pageSize: 10,
            selItem: null,
            pageTotal: 0,
            showEdit: false,
            editItem: null,
            isAdd: false,
            isShow: false,
            imgUrl: '',
            content: '',
            action: '',
            isShowPop: '',
            editType: 0, // 0:新建  1:编辑  2:重建
            timeRange: null
        };
    },
    created() {
        this.getData();
    },
    filters: {
        statusFilter(v) {
            let val = v || 0;
            if (val == '0') {
                return '未发布';
            } else if (val == '1') {
                return '已发布';
            } else if (val == '-1') {
                return '已撤回';
            }
        }
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.$api
                .post({
                    url: '/healthArticle/pageQuery',
                    data: {
                        pageNo: this.pageNo,
                        pageSize: this.pageSize,
                        param: {
                            title: this.title,
                            status: this.status == '全部' ? null : this.status,
                            createTimeStart: this.timeRange ? this.timeRange[0] + ' 00:00:00' : null,
                            createTimeEnd: this.timeRange ? this.timeRange[1] + ' 00:00:00' : null,
                            authorName: this.authorName,
                            from: this.from
                        }
                    }
                })
                .then((res) => {
                    if (res.code && res.code == '1001') {
                    } else {
                        this.tableData = res.data.result || [];
                        this.pageTotal = res.data.total;
                    }
                });
        },
        // 触发搜索按钮
        handleSearch() {
            this.pageNo = 1;
            this.getData();
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.showEdit = true;
            this.bannerId = row.id;
            this.editType = 1;
        },
        handleRebuild(index, row) {
            this.showEdit = true;
            this.bannerId = row.id;
            this.editType = 2;
        },
        // 预览
        handlePre(index, item) {
            let self = this;
            self.$api
                .get({
                    url: '/healthArticle/findById',
                    data: {
                        id: item.id
                    }
                })
                .then((res) => {
                    console.log('🚀 ~ file: editBanner.vue ~ line 156 ~ getData ~ res', res);
                    // self.title = res.data.title || '';
                    // self.authorName = res.data.authorName || '';
                    // self.from = res.data.from || '';
                    let content = res.data.content || '';
                    self.content = content.replace(/\<img/gi, '<img class="image" style="max-width:330px"');

                    if (res.data.bannerFile) {
                        self.imgUrl = 'https://uhome.haier.net/acecology/hsadmin/static/' + res.data.bannerFile.path;
                    }
                    self.selItem = item;
                    self.isShow = true;
                })
                .catch((err) => {});
        },
        handleAction(item, action) {
            this.selItem = item;
            this.isShowPop = true;
            this.action = action;
        },
        goAction() {
            let url = '';
            if (this.action == '发布') {
                url = '/healthArticle/release';
            } else if (this.action == '撤回') {
                url = '/healthArticle/withdraw';
            } else if (this.action == '删除') {
                url = '/healthArticle/del';
            }
            this.$api
                .get({
                    url: url,
                    data: {
                        id: this.selItem.id
                    }
                })
                .then((res) => {
                    if (res.code == 200) {
                        this.getData();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                    this.isShowPop = false;
                });
        },
        // 新增
        addAccount() {
            this.showEdit = true;
            this.editType = 0;
            this.bannerId = 0;
        },
        back() {
            this.selItem = null;
            this.showEdit = false;
        },
        // 保存
        save() {
            this.selItem = null;
            this.showEdit = false;
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.pageNo = val;
            this.getData();
        },
        methodIndex(index) {
            return index + 1;
        }
    },
    components: {
        editHealthBanner
    }
};
</script>

<style lang="scss" scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 100px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
/deep/.popView {
    display: flex;
    align-items: center;
    justify-content: center;
    .el-dialog__header {
        padding: 0;
    }
    .el-dialog__body {
        padding: 0;
    }
    .main {
        display: flex;
        flex-direction: column;
        align-items: center;
        background-color: #fff;
        border-radius: 5px;
        .title {
            width: 100%;
            font-size: 17px;
            font-weight: bold;
            margin-bottom: 20px;
            text-align: center;
        }
        .btns {
            width: 100%;
            display: flex;
            justify-content: space-around;
        }
    }
}
/deep/.popView1 {
    .el-dialog__header {
        padding-top: 10px;
        text-align: center;
    }
    .el-dialog__body {
        padding: 0 20px 20px 20px;
    }
}
.preView {
    .el-dialog {
        border-radius: 20px;
        .main {
            position: relative;
            .title {
                width: 100%;
                text-align: center;
                font-size: 14px;
                padding: 0 12px;
                box-sizing: border-box;
                text-overflow: ellipsis;
                white-space: nowrap;
                overflow: hidden;
            }
            .web {
                position: absolute;
                top: -40px;
                height: 620px;
            }
        }
    }
}
</style>
